﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate : MonoBehaviour
{
        public enum RotationAxes
        {
                //X和Y轴可用
                MouseXAndY = 0,
                //X轴可用
                MouseX = 1,
                //Y轴可用
                MouseY = 2
        }

        [Header("旋转轴")]
        public RotationAxes axes = RotationAxes.MouseXAndY;
        [Header("X轴旋转灵敏度")]
        public float sensitivityX = 7f;
        [Header("Y轴旋转灵敏度")]
        public float sensitivityY = 3f;
        [Header("X轴最小角度")]
        public float minimumX = -360f;
        [Header("X轴最大角度")]
        public float maximumX = 360f;
        [Header("Y轴最小角度")]
        public float minimumY = -90F;
        [Header("Y轴最大角度")]
        public float maximumY = -30F;
        [Header("是否可用")]
        public bool active = false;

        private float xInput = 0f;
        private float yInput = 0f;
        private float xInputOld = 0f;
        private float yInputOld = 0f;
        private float averageXInput = 0f;
        private float averageYInput = 0f;
        private float rotationX = 0f;
        private float rotationY = 0f;
        private Quaternion originalRotation;

        void Start()
        {
                originalRotation = Quaternion.identity;
        }

        public void MouseDown()
        {
                active = true;
        }

        public void MouseUp()
        {
                active = false;
        }

        void Update()
        {
                if (active)
                {
                        if (axes == RotationAxes.MouseXAndY)
                        {

                                // 备份
                                xInputOld = xInput;
                                yInputOld = yInput;

                                // 读取当前鼠标位移
                                xInput = Input.GetAxis("Mouse X") * sensitivityX;
                                yInput = Input.GetAxis("Mouse Y") * sensitivityY;

                                //中和新旧旋转
                                averageXInput = xInput + xInputOld;
                                averageYInput = yInput + yInputOld;
                                averageXInput *= 0.5f;
                                averageYInput *= 0.5f;

                                rotationX += averageXInput;
                                rotationY += averageYInput;

                                rotationX = ClampAngle(rotationX, minimumX, maximumX);
                                rotationY = ClampAngle(rotationY, minimumY, maximumY);

                                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);

                                transform.localRotation = originalRotation * xQuaternion * yQuaternion;

                        }
                        else if (axes == RotationAxes.MouseX)
                        {

                                // 备份
                                xInputOld = xInput;

                                // 读取当前鼠标位移
                                xInput = Input.GetAxis("Mouse X") * sensitivityX;

                                // 中和新旧旋转
                                averageXInput = xInput + xInputOld;
                                averageXInput *= 0.5f;

                                rotationX += averageXInput;
                                rotationX = ClampAngle(rotationX, minimumX, maximumX);

                                Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
                                transform.localRotation = originalRotation * xQuaternion;

                        }
                        else
                        {

                                // 备份
                                yInputOld = yInput;

                                // 读取当前鼠标位移
                                yInput = Input.GetAxis("Mouse Y") * sensitivityY;

                                // 中和新旧旋转
                                averageYInput = yInput + yInputOld;
                                averageYInput *= 0.5f;

                                rotationY += averageYInput;

                                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                                rotationY = ClampAngle(rotationY, minimumY, maximumY);

                                Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left);
                                transform.localRotation = originalRotation * yQuaternion;
                        }
                }
        }

        public static float ClampAngle(float angle, float min, float max)
        {
                if (angle < -360F) angle += 360F;
                if (angle > 360F) angle -= 360F;
                return Mathf.Clamp(angle, min, max);
        }
}
